package com.liruan.rest.service.result;

import java.util.HashSet;
import java.util.Set;

/**
 * 业务流程中的错误码定义
 *
 */
public enum ResultCode
{

	GENERAL_SUCCESS(ResultCode.GENERAL_SUCCESS_CODE, ResultCode.GENERAL_SUCCESS_TIP),
	GENERAL_FAIL(ResultCode.GENERAL_FAIL_CODE, ResultCode.GENERAL_FAIL_TIP),
	NO_SUCH_RECORD(ResultCode.NO_SUCH_RECORD_CODE, ResultCode.NO_SUCH_RECORD_TIP),
	DB_ACESSS_FAIL(ResultCode.DB_ACESSS_FAIL_CODE, ResultCode.DB_ACESSS_FAIL_TIP),
	SEARCH_CONDITION_NOT_ENOUGH(ResultCode.SEARCH_CONDITION_NOT_ENOUGH_CODE, ResultCode.SEARCH_CONDITION_NOT_ENOUGH_TIP),
	NOT_INVALID_NODE(ResultCode.NOT_INVALID_NODE_CODE, ResultCode.NOT_INVALID_NODE_TIP),
	NULL_PARAM(ResultCode.NULL_PARAM_CODE, ResultCode.NULL_PARAM_TIP),
	NO_SUCH_USER(ResultCode.NO_SUCH_USER_CODE, ResultCode.NO_SUCH_USER_TIP),
	INVALID_PARAM(ResultCode.INVALID_PARAM_CODE, ResultCode.INVALID_PARAM_TIP),
	EXCESS_AUTH(ResultCode.EXCESS_AUTH_CODE, ResultCode.EXCESS_AUTH_TIP),
	AUTH_FAIL(ResultCode.AUTH_FAIL_CODE, ResultCode.AUTH_FAIL_TIP),
	DUP_USER(ResultCode.DUP_USER_CODE, ResultCode.DUP_USER_TIP),	
	
	DB_DUP_KEY(ResultCode.DB_DUP_KEY_CODE, ResultCode.DB_DUP_KEY_TIP);

	public static final int GENERAL_SUCCESS_CODE = 10000;
	public static final String GENERAL_SUCCESS_TIP = "调用成功!";

	public static final int GENERAL_FAIL_CODE = 20001;
	public static final String GENERAL_FAIL_TIP = "调用失败!";

	public static final int NO_SUCH_RECORD_CODE = 20002;
	public static final String NO_SUCH_RECORD_TIP = "未能在系统中查询到相应记录!";

	public static final int DB_ACESSS_FAIL_CODE = 20003;
	public static final String DB_ACESSS_FAIL_TIP = "数据库访问错误！";

	public static final int SEARCH_CONDITION_NOT_ENOUGH_CODE = 20004;
	public static final String SEARCH_CONDITION_NOT_ENOUGH_TIP = "查询条件不足";

	public static final int DB_DUP_KEY_CODE = 20005;
	public static final String DB_DUP_KEY_TIP = "无法插入重复的记录!";

	public static final int NOT_INVALID_NODE_CODE = 20006;
	public static final String NOT_INVALID_NODE_TIP = "输入的行政区码不合法!";

	public static final int NULL_PARAM_CODE = 20007;
	public static final String NULL_PARAM_TIP = "必须的输入参数为NULL!";
	
	public static final int NO_SUCH_USER_CODE = 20008;
	public static final String NO_SUCH_USER_TIP = "没有该用户!";
	
	public static final int INVALID_PARAM_CODE = 20009;
	public static final String INVALID_PARAM_TIP = "非法的参数值!";
	
	public static final int EXCESS_AUTH_CODE = 20010;
	public static final String EXCESS_AUTH_TIP = "越权的操作!";
	
	public static final int AUTH_FAIL_CODE = 20011;
	public static final String AUTH_FAIL_TIP = "登录授权失败!";
	
	public static final int DUP_USER_CODE = 20012;
	public static final String DUP_USER_TIP = "注册用户信息已存在";

	private static Set<Integer> ALL_CODES;

	private static void registerCode(ResultCode code)
	{
		synchronized (ResultCode.class)
		{
			Integer tmp = code.code;

			if (ALL_CODES == null)
			{
				ALL_CODES = new HashSet<>();
			}

			if (ALL_CODES.contains(tmp))
			{
				throw new RuntimeException("重复的错误码:" + tmp);
			}

			ALL_CODES.add(tmp);
		}
	}

	private final int code;
	private final String desc;

	private ResultCode(int code, String statusDesc)
	{
		this.code = code;
		this.desc = statusDesc;
		registerCode(this);
	}

	public int getCode()
	{
		return code;
	}

	public String getDesc()
	{
		return this.desc;
	}
	
	
}
